package com.launcher.wkang.activity;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.KeyguardManager;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.PowerManager;
import android.os.SystemClock;
import android.provider.Settings;
import android.support.annotation.RequiresApi;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.launcher.wkang.App;
import com.launcher.wkang.R;

import java.lang.reflect.InvocationTargetException;

public class CharacteristicFunctionActivity extends Activity implements View.OnClickListener {
    private TextView clockLockScreen;
    private TextView lightAdjustment;
    private TextView  classScheduleCard;
    private TextView  personalInformation;
    private TextView  powerSavingMode;
    private static final int DEVICE_POWER =100;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_characteristic_function);
        initView();
        initData();
    }

    @SuppressLint("NewApi")
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.clock_lock_screen:
                goToSleep(this);

//                if(checkSelfPermission("android.permission.DEVICE_POWER")!=PackageManager.PERMISSION_GRANTED){
//                    // 申请一个（或多个）权限，并提供用于回调返回的获取码（用户定义）
//                    requestPermissions( new String[]{ "android.permission.DEVICE_POWER" },DEVICE_POWER );
//                }else {
////                    initBaidu();
//                    goToSleep(this);
//                }
                break;
            case R.id.light_adjustment:
                Intent lightAdjustmentIntent=new Intent(this,ScreenBrightnessActivity.class);
                startActivity(lightAdjustmentIntent);
                break;
            case R.id.class_schedule_card:
                Intent scheduleCardIntent=new Intent();
                //把返回数据存入Intent
                scheduleCardIntent.putExtra("result", 1);
                //设置返回数据
                setResult(RESULT_OK, scheduleCardIntent);
                finish();
                break;
            case R.id.personal_information:
                Intent personalInformationIntent=new Intent();
                //把返回数据存入Intent
                personalInformationIntent.putExtra("result", 2);
                //设置返回数据
                setResult(RESULT_OK, personalInformationIntent);
                finish();
                break;
            case R.id.power_saving_mode:
                ComponentName componet = new ComponentName("com.android.settings.fuelgauge", "com.android.settings.fuelgauge.BatterySaverSettings");
                Intent i = new Intent();
                i.setComponent(componet);
//                startActivity(i);

                Settings.System.putString(getContentResolver(),Settings.System.AIRPLANE_MODE_ON, "1");
                Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
                sendBroadcast(intent);

                break;
        }

    }
    private void initView(){
        clockLockScreen=findViewById(R.id.clock_lock_screen);
        clockLockScreen.setOnClickListener(this);
        lightAdjustment=findViewById(R.id.light_adjustment);
        lightAdjustment.setOnClickListener(this);
        classScheduleCard=findViewById(R.id.class_schedule_card);
        classScheduleCard.setOnClickListener(this);
        personalInformation=findViewById(R.id.personal_information);
        personalInformation.setOnClickListener(this);
        powerSavingMode=findViewById(R.id.power_saving_mode);
        powerSavingMode.setOnClickListener(this);
    }
    private void initData(){

    }

    @Override
    public void onRequestPermissionsResult(int requestCode,String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions,grantResults);
        switch(requestCode) {
            //requestCode即所声明的权限获取码，在checkSelfPermission时传入
            case DEVICE_POWER:
                if(grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                 //   goToSleep(this);
                    //获取到权限，做相应处理
                    //调用定位SDK应确保相关权限均被授权，否则会引起定位失败
                } else{
                    //没有获取到权限，做特殊处理
                }
                break;
            default:
                break;
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public void goToSleep(Context context) {
//        Toast.makeText(context,"xxxxxx",Toast.LENGTH_LONG).show();


        DevicePolicyManager  mDevicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
        mDevicePolicyManager.lockNow();
/*
        KeyguardManager mKeyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);

        Intent intent = mKeyguardManager.createConfirmDeviceCredentialIntent(null, null);
        if (intent != null) {
            startActivityForResult(intent, 1024);
        }*/



  /*      PowerManager powerManager= (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        try {

            powerManager.getClass().getMethod("goToSleep", new Class[]{long.class}).invoke(powerManager, SystemClock.uptimeMillis());

        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        catch (NoSuchMethodException e) {
            e.printStackTrace();
        }*/
    }
    @Override
    protected void onResume() {
        super.onResume();
        App.getINstance().setsetWaveformMode(2,1);
        new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
            @Override
            public void run() {
                App.getINstance().setsetWaveformMode(6,1);
            }
        },1000);

    }

}
